﻿
/*
 * Javasript based on Functions(JBF) 1.03
 * 
 * Copyright(c) 2010, Fooxee (JA86.CN)
 *
 * JBF is licensed under the terms of the Open Source GPL 3.0(http://www.gnu.org/licenses/gpl.html) license. 
 * 
 * JBF's blog: http://www.cnblogs.com/JBF/
 * 
 * Created by Fooxee. at 2010.03.08  mailTo:hjccjun@163.com
 */
 
 //兼容
window.undefined = window.undefined;


//JBF v1.03
var JBF = window.JBF = window.jbf = window.$$ = function()
{
	if(JBF.Dom)
	{
		//调用DOM操作
		return JBF.Dom.apply(this, arguments);
	}
	else
	{
		//未包含DOM操作
		return typeof arguments[0] =="string" ? document.getElementById(arguments[0]) : arguments[0];
	}
};


//扩展(目标,源)
JBF.apply = function(d,s)
{
    if(d && s && typeof s == 'object')
	{
        for(var p in s)
		{
            d[p] = s[p];
        }
    }
    return d;
};

(function(){
	var idSeed = 0,
	toString = Object.prototype.toString,
	ua = navigator.userAgent.toLowerCase(),
	check = function(r){return r.test(ua);},
	DOC = document,
	baseURL,
	isStrict = DOC.compatMode == "CSS1Compat",
	isOpera = check(/opera/),
	isChrome = check(/chrome/),
	isWebKit = check(/webkit/),
	isSafari = !isChrome && check(/safari/),
	isSafari2 = isSafari && check(/applewebkit\/4/), // unique to Safari 2
	isSafari3 = isSafari && check(/version\/3/),
	isSafari4 = isSafari && check(/version\/4/),
	isIE = !isOpera && check(/msie/),
	isIE7 = isIE && check(/msie 7/),
	isIE8 = isIE && check(/msie 8/),
	isIE6 = isIE && !isIE7 && !isIE8,
	isGecko = !isWebKit && check(/gecko/),
	isGecko2 = isGecko && check(/rv:1\.8/),
	isGecko3 = isGecko && check(/rv:1\.9/),
	isFF = isGecko && check(/firefox/),
	isFF2 = isFF && check(/firefox\/2/),
	isFF3 = isFF && check(/firefox\/3/),
	isBorderBox = isIE && !isStrict,
	isWindows = check(/windows|win32/),
	isMac = check(/macintosh|mac os x/),
	isAir = check(/adobeair/),
	isLinux = check(/linux/),
	isSecure = /^https/i.test(window.location.protocol);

	// remove css image flicker
	if(isIE6){
	try{
	DOC.execCommand("BackgroundImageCache", false, true);
	}catch(e){}
	}
	
	//The root relative path to jbf.js (as a string)
	if(this["document"] && this["document"]["getElementsByTagName"])
	{
		var scripts = document.getElementsByTagName("script");
		var rePkg = /jbf\.js(\W|$)/i;
		for(var i = 0; i < scripts.length; i++)
		{
			var src = scripts[i].getAttribute("src");
			if(!src){ continue; }
			var m = src.match(rePkg);
			if(m)
			{
				baseURL=src.substring(0, m.index);
			}
		}
	}
	
	//JBF
	JBF.apply(JBF, {
		//版本号
		version : '1.03',
		idSeed : idSeed,
		zIndex:0,
		baseURL: baseURL,
		//空函数
		fn : function(){},
		//空函数
		emptyFn : function(){},
		
		//创建类(构造方法,object)
		createClass : function()
		{
			var init,o;
			if(JBF.isFunction(arguments[0]))
			{
				o=arguments[1] || {};
				init=arguments[0] ||o.init || function(){};
				
			}
			else if(JBF.isObject(arguments[0]))
			{
				o=arguments[0];
				init=o.init || function(){};
			}
			o=o || {};
			o.init= init || function(){};
			o.init.prototype=o;
			return o.init;
		},
		
		//邦定
		bind : function(o, f)
		{
			return function()
			{
				return f.apply(o, arguments);
			}
		},
		
		//遍历
		each : function(l, f,o)
		{
			var i=0, len = l.length, n;
			if(len == undefined)
			{
				for(n in l) f.call(o||this,n, i++, l);
			}
			else
			{
				for( ; i<len ;) f.call(o||this,l[i++], i-1, l);
			}
		},
		//不覆盖扩展
		applyIf : function(d,s)
		{
			if(d && s && typeof s == 'object')
			{
				for(var p in s)
				{
					if(!JBF.isDefined(d[p]))
					{
						d[p] = s[p];
					}
				}
			}
			return d;
		},
		
		//对象长度(字符串中文两字节,英文1个字节)
		len : function(o)
		{
			var i,sum=0;
			if(this.isObject(o))
			{
				if(this.isNumber(o.length))
				{
					return 	o.length;
				}
				else
				{
					this.each(o,function(k,i)
					{
						sum=i;
					});
					return sum+1;
				}
				
			}
			else if(this.isString(o))
			{
				for(i=0;i<o.length;i++)
				{
					if ((o.charCodeAt(i)>=0) && (o.charCodeAt(i)<=255))
					{
						sum=sum+1;
					}
					else
					{
						sum=sum+2;
					}
				}
				return sum;
			}
		},
		
		//转换为数组
		toArray : function()
		{
             return isIE ?
                 function(a, i, j, res)
				 {
                     res = [];
                     for(var x = 0, len = a.length; x < len; x++)
					 {
                         res.push(a[x]);
                     }
                     return res.slice(i || 0, j || res.length);
                 } :
                 function(a, i, j){
                     return Array.prototype.slice.call(a, i || 0, j || a.length);
                 }
         }(),
		
		//json字符串转换为对象
		jsonDecode : function(json)
		{
			return eval('('+json+')');
		},
		
		get : function()
		{
			//id
			//name
			//p z
		},
		createRequest : function()
		{
			try {return new ActiveXObject("Microsoft.XMLHTTP");}
			catch(e) {try {return new ActiveXObject("Msxml2.XMLHTTP");}
			catch(e) {try {return new XMLHttpRequest();}catch(e) {return window.createRequest();};};};
		},
		//JBF模块
		modules:{},
		//已加载的模块脚本文件
		loadedScripts:[],
		//模块注册
		provide:function(p,src)
		{
			if(!(this.isString(p) || this.isObject(p)) || !this.isString(src))
			{
				this.debug(new Error(10,'include error: 模块注册错误'));
				return;	
			}
			if(this.isObject(p))
			{
				this.each(p,function(v,i,list)
				{
					if(this.isString(v))
					{
						JBF.modules[v]=src;
					}
				},this);
			}
			else
			{
				JBF.modules[p]=src;
			}
		},
		//引用
		inc:function(n)
		{
			this.include(n)
		},
		incIf:function(n)
		{
			this.include(n,true);
		},
		includeIf:function(n)
		{
			this.include(n,true);
		},
		//引用
		include:function(n,mod)
		{
			if(!this.isPrimitive(n))
			{
				
			}
			var arr=n.split('.*');
			
			if(this.len(arr)==1)
			{
				var s=this.modules[arr[0]];
				if(this.isString(s))
				{
					this.includeScript(baseURL+s,mod,n);
				}
				else
				{
					this.debug(new Error(10,'include error: ['+n+'] 未定义'));
				}
				return;
			}
			else if(this.len(arr)==2)
			{
				this.each(this.modules,function(v,i,list)
				{
					if(v.indexOf(arr[0])==0)
					{
						var s=list[v];
						if(this.isString(s))
						{
							this.includeScript(baseURL+s,mod,n);
						}
						else
						{
							this.debug(new Error(10,'include error: ['+n+'] 未定义'));
						}
					}
				},this);
			}
			else
			{
				this.debug(new Error(10,'include error: ['+n+'] 未定义'));
			}
			
			
		},
		//private
		includeScript:function(src,mod,n)
		{
			try
			{
				var isReturn=false;
				if(mod)
				{
					this.each(this.loadedScripts,function(v,i,list)
					{
						if(v==src)
							isReturn=true;
					},this);
				}
				if(isReturn)
					return;
				
				var xhr = this.createRequest();
				xhr.open("GET", src, false);
				xhr.send();
				eval(xhr.responseText);
				this.loadedScripts.push(src);
				//this.included.call(this,src,n,mod);
			}
			catch(e)
			{
				this.debug(e);
			}
		},
		//引用事件
		//included:this.emptyFn,
		//Console Error
		debug : function(e,mod)
		{
			mod=mod||0;
			if(mod==0)
			{
				var putmsg	='JBF debug Console Error:';
					putmsg +='\n错误代码:'+e.number;
					putmsg +='\n错误类型:'+e.name;
					putmsg +='\n错误行号:'+e.lineNumber;
					putmsg +='\n详细信息:'+e.message;
				alert(putmsg);
			}
			else if(mod==1)
			{
				throw e;
			}
		},
		//是否为空
		isNull:function(v)
		{
			return v === null || v === undefined ||  v === '';
		},
		//是否为空
		isEmpty : function(v, allowBlank)
		{
			return v === null || v === undefined || ((JBF.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
		},
		
		//是否是数组
        isArray : function(v)
		{
            return toString.apply(v) === '[object Array]';
        },

        //是否是日期
        isDate : function(v)
		{
            return toString.apply(v) === '[object Date]';
        },
		
		//是否是对象(已过时)
		isObj : function(v)
		{
			return typeof v=='object';
		},
        //是否是对象
        isObject : function(v)
		{
            return typeof v=='object';
        },

        //是否是原始类型(不可再细分的)
        isPrimitive : function(v)
		{
            return JBF.isString(v) || JBF.isNumber(v) || JBF.isBoolean(v);
        },
		
		
		//是否是函数
        isFn : function(v)
		{
            return toString.apply(v) === '[object Function]';
        },
        //是否是函数
        isFunction : function(v)
		{
            return toString.apply(v) === '[object Function]';
        },

        //是否是数字
        isNum : function(v)
		{
            return typeof v === 'number' && isFinite(v);
        },
		//是否是数字
        isNumber : function(v)
		{
            return typeof v === 'number' && isFinite(v);
        },
		
		//是否是字符串
		isStr : function(v)
		{
			return JBF.isString(v);
		},
        //是否是字符串
        isString : function(v)
		{
            return typeof v === 'string';
        },
		
		//是否是布尔值
		isBool : function(v)
		{
			return JBF.isBoolean(v);
		},
        //是否是布尔值
        isBoolean : function(v)
		{
            return typeof v === 'boolean';
        },
		
		//是否是DOM元素
        isDom : function(v)
		{
            return JBF.isElement(v);
        },
        //是否是DOM元素
        isElement : function(v)
		{
            return !!v && v.tagName;
        },
		
		isXMLDoc: function(v)
		{
			return v.documentElement && !v.body || v.tagName && v.ownerDocument && !v.ownerDocument.body;
		},
		
        //是否定义
        isDefined : function(v)
		{
            return typeof v !== 'undefined';
        },
		
		//是否是电子邮箱
		isEmail:function(v)
		{
			return /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,4}$/.test(v);
		},
		isURL : function(s)
		{
			var m = "^((https|http|ftp|rtsp|mms)?://)" 
			+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp user@ 
			+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" // IPv4
			+ "|" 
			+ "([0-9a-z_!~*'()-]+\.)*" // 域名- www. 
			+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." // 二级域名 
			+ "[a-z]{2,6})" 
			+ "(:[0-9]{1,4})?" // 端口- :80 
			+ "((/?)|" // 无文件名 /?
			+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
			return new RegExp(m).test(s);
		},
		
        //浏览器相关
        isOpera : isOpera,
        isWebKit : isWebKit,
        isChrome : isChrome,
        isSafari : isSafari,
		isSafari2 : isSafari2,
        isSafari3 : isSafari3,
        isSafari4 : isSafari4,
        isIE : isIE,
        isIE6 : isIE6,
        isIE7 : isIE7,
        isIE8 : isIE8,
        isGecko : isGecko,
        isGecko2 : isGecko2,
        isGecko3 : isGecko3,
		isFF : isFF,
		isFF2 : isFF2,
		isFF3 : isFF3,
        isBorderBox : isBorderBox,
        isLinux : isLinux,
        isWindows : isWindows,
        isMac : isMac,
        isAir : isAir,
		screen : window.screen
	});
	
	try
	{
		var xhr = JBF.createRequest();
		xhr.open("GET", baseURL+'core/config.js', false);
		xhr.send();
		eval(xhr.responseText);
	}
	catch(e)
	{
		this.debug(e);
	}
	
})();

JBF.apply(JBF,{  
	//ready  
	//private
	readyCache : [],
	readyTimer : false,
	
	isReady : false,
	ready : function(handler)
	{
		this.pollReady();
		if(!JBF.isFunction(handler))
		{
			return;
		}
		if(this.isReady)
		{
			handler();
		}
		else
		{
			this.readyCache.push(handler);	
		}
	},
	//private
	pollReady : function()
	{
		if(arguments.callee.one)
		{
			return;
		}
		arguments.callee.one = true;
		//var fire=JBF.Dom.fireReady;
		//alert(fire)
		//ie
		if(JBF.isIE && self === self.top)
		{
			(function(){
				if (JBF.isReady) return;
				try
				{document.documentElement.doScroll("left");} catch(e)
				{
					JBF.readyTimer=setTimeout(arguments.callee,0);
					return;
				}
				JBF.Dom.ready();
			})();
			document.onreadystatechange = function()
			{
				if (document.readyState === 'complete')
				{
					document.onreadystatechange = null;//清除加载函数
					JBF.fireReady();
				}
			};
		}
		else
		{
			document.addEventListener("DOMContentLoaded", function()
			{
				document.removeEventListener( "DOMContentLoaded", arguments.callee, false );//清除加载函数
				JBF.fireReady();
			}, false);
		}
	},
	fireReady : function()
	{
		JBF.isReady=true;
		JBF.each(JBF.readyCache,function(v,i,list)
		{
			v.call(window);
		});
		if (JBF.readyTimer){window.clearTimeout(JBF.readyTimer);}
	}
		   
});
String.prototype.trim=function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
//扩展字符串对象
JBF.applyIf(String, {
	trims : function()
	{
		return this.replace(/(^\s*)|(\s*$)/g, "");
	},
	//格式化字符串
    format : function(format)
	{
        var args = JBF.toArray(arguments, 1);
        return format.replace(/\{(\d+)\}/g, function(m, i)
		{
            return args[i];
        });
    }
});


/*

String.prototype.Trim = function() 
{ 
return this.replace(/(^\s*)|(\s*$)/g, ""); 
} 
String.prototype.LTrim = function() 
{ 
return this.replace(/(^\s*)/g, ""); 
} 
String.prototype.RTrim = function() 
{ 
return this.replace(/(\s*$)/g, ""); 
} 


*/

//扩展数组对象
JBF.applyIf(Array.prototype, {
    //
    indexOf : function(o, from)
	{
        var len = this.length;
        from = from || 0;
        from += (from < 0) ? len : 0;
        for (; from < len; ++from)
		{
            if(this[from] === o)
			{
                return from;
            }
        }
        return -1;
    },

    //移除对象
    remove : function(o)
	{
       var index = this.indexOf(o);
        if(index != -1)
		{
            this.splice(index, 1);
        }
        return this;/* */
    }
});



